package com.ibiaoke.order.dao;

import com.ibiaoke.common.base.BaseDao;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.customer.entity.XcxOpenGroupExample;
import com.ibiaoke.order.consts.XcxOrderStatusConst;
import com.ibiaoke.order.entity.XcxOrder;
import com.ibiaoke.order.entity.XcxOrderExample;
import com.ibiaoke.order.entity.so.XcxOrderSO;
import com.ibiaoke.order.entity.vo.XcxOrderVO;
import com.ibiaoke.order.mapper.XcxOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author Healer
 * @date 2018/3/7/17:02;
 */
@Repository
public class XcxOrderDao extends BaseDao<XcxOrder, XcxOrderExample, Integer, XcxOrderMapper> {

    @Autowired
    private XcxOrderMapper xcxOrderMapper;

    @Override
    public XcxOrderMapper getMapper() {
        return xcxOrderMapper;
    }

    public List<XcxOrder> xcxOrderList(XcxOrderVO xcxOrder, Page page) {
        XcxOrderExample example = new XcxOrderExample();
        XcxOrderExample.Criteria criteria = example.createCriteria();

        //用户ID
        if (xcxOrder.getUserId() != null) {
            criteria.andUserIdEqualTo(xcxOrder.getUserId());
        }
        //订单编号
        if (xcxOrder.getOrderCode() != null) {
            criteria.andOrderCodeEqualTo(xcxOrder.getOrderCode());
        }
        //商品ID
        if (xcxOrder.getGoodId() != null) {
            criteria.andGoodIdEqualTo(xcxOrder.getGoodId());
        }
        //订单状态
        if (xcxOrder.getOrderStatus() != null) {
            if(xcxOrder.getOrderStatus().length() < 2){
                criteria.andOrderStatusEqualTo(Integer.valueOf(xcxOrder.getOrderStatus()));
            }else{
                String[] strarray = xcxOrder.getOrderStatus().split(",");
                List<Integer> list = new ArrayList<>();
                for(String  orderStatus: strarray){
                    list.add(Integer.valueOf(orderStatus));
                }
                criteria.andOrderStatusIn(list);
            }
        }
        //支付方式
        if (xcxOrder.getPayType() != null) {
            criteria.andPayTypeEqualTo(xcxOrder.getPayType());
        }
        //订单类型
        if (xcxOrder.getOrderType() != null) {
            criteria.andOrderTypeEqualTo(xcxOrder.getOrderType());
        }
        //配送类型
        if (xcxOrder.getDeliveryType() != null) {
            criteria.andDeliveryTypeEqualTo(xcxOrder.getDeliveryType());
        }
        //自提门店ID
        if (xcxOrder.getShopId() != null) {
            criteria.andShopIdEqualTo(xcxOrder.getShopId());
        }
        //手机号码
        if (xcxOrder.getContactMobile() != null) {
            criteria.andContactMobileEqualTo(xcxOrder.getContactMobile());
        }
        //订单来源
        if (xcxOrder.getSource() != null) {
            criteria.andSourceEqualTo(xcxOrder.getSource());
        }
        //订单开始时间
        if (xcxOrder.getCreateTime() != null) {
            criteria.andCreateTimeGreaterThanOrEqualTo(xcxOrder.getCreateTime());
        }
        //订单结束时间
        if (xcxOrder.getEndTime() != null) {
            criteria.andEndTimeLessThan(xcxOrder.getEndTime());
        }
        criteria.andDelStatusEqualTo(new Byte("0"));
        if (page != null) {
            page.setTotalCount(xcxOrderMapper.countByExample(example));
            example.setLimitStart(page.limitStart());
            example.setLimitEnd(page.limitEnd());
        }
        example.setOrderByClause("create_time desc");
        return xcxOrderMapper.selectByExample(example);
    }

    public List<XcxOrder> xcxPtOrderList(XcxOrderSO xcxOrderSO, Page page) {
        XcxOrderExample example = new XcxOrderExample();
        XcxOrderExample.Criteria criteria = example.createCriteria();

        //用户ID
        if (xcxOrderSO.getUserId() != null) {
            criteria.andUserIdEqualTo(xcxOrderSO.getUserId());
        }
        //订单编号
        if (xcxOrderSO.getOrderCode() != null) {
            criteria.andOrderCodeEqualTo(xcxOrderSO.getOrderCode());
        }
        //商品ID
        if (xcxOrderSO.getGoodId() != null) {
            criteria.andGoodIdEqualTo(xcxOrderSO.getGoodId());
        }
        //订单状态
        if (xcxOrderSO.getOrderStatus() != null) {
            criteria.andOrderStatusEqualTo(xcxOrderSO.getOrderStatus());
        }
        //支付方式
        if (xcxOrderSO.getPayType() != null) {
            criteria.andPayTypeEqualTo(xcxOrderSO.getUserId());
        }
        //配送类型
        if (xcxOrderSO.getDeliveryType() != null) {
            criteria.andDeliveryTypeEqualTo(xcxOrderSO.getDeliveryType());
        }
        //自提门店ID
        if (xcxOrderSO.getShopId() != null) {
            criteria.andShopIdEqualTo(xcxOrderSO.getShopId());
        }
        //订单来源
        if (xcxOrderSO.getSource() != null) {
            criteria.andSourceEqualTo(xcxOrderSO.getSource());
        }
        //订单开始时间
        if (xcxOrderSO.getCreateTime() != null) {
            criteria.andCreateTimeGreaterThanOrEqualTo(xcxOrderSO.getCreateTime());
        }
        //订单结束时间
        if (xcxOrderSO.getEndTime() != null) {
            criteria.andEndTimeLessThan(xcxOrderSO.getEndTime());
        }
        if (page != null) {
            page.setTotalCount(xcxOrderMapper.countByExample(example));
            example.setLimitStart(page.limitStart());
            example.setLimitEnd(page.limitEnd());
        }
        example.setOrderByClause("create_time desc");
        return xcxOrderMapper.selectByExample(example);
    }

    public int ConfirmOrder(Customer customer, XcxOrder xcxOrder) {
        XcxOrderExample example = new XcxOrderExample();
        example.createCriteria()
                .andOrderIdEqualTo(xcxOrder.getOrderId())
                .andUserIdEqualTo(customer.getCustomerId())
                .andOrderStatusIn(Arrays.asList(XcxOrderStatusConst.PAID, XcxOrderStatusConst.DELIVERED));

        XcxOrder xcxOrders = new XcxOrder();
        xcxOrders.setOrderStatus(XcxOrderStatusConst.RECEIVED);
        xcxOrders.setReceivedRecord(xcxOrder.getReceivedRecord());
        xcxOrders.setRecordTime(xcxOrder.getRecordTime());
        xcxOrders.setServicenum(xcxOrder.getServicenum());

        return xcxOrderMapper.updateByExampleSelective(xcxOrders, example);
    }



    public int delXcxOrder(Integer customerId, Integer orderId) {
        XcxOrderExample example = new XcxOrderExample();
        example.createCriteria().andUserIdEqualTo(customerId).andOrderIdEqualTo(orderId);

        XcxOrder xcxOrders = new XcxOrder();
        xcxOrders.setDelStatus((byte) XcxOrderStatusConst.YES);
        return xcxOrderMapper.updateByExampleSelective(xcxOrders, example);
    }

    public int saveXcxOrder(XcxOrder xcxOrder) {
        return xcxOrderMapper.insert(xcxOrder);
    }

    public void updateXcxOrderStatus(XcxOrder xcxOrder) {
        xcxOrder.setOrderStatus(4);
        updateByPrimaryKeySelective(xcxOrder);
    }

    public int updatePayTimeAndUpdateTime(XcxOrder xcxOrder) {
        xcxOrder.setPayTime(new Date());
        xcxOrder.setUpdateTime(new Date());
        return updateByPrimaryKeySelective(xcxOrder);
    }

    public List<XcxOrder> getXcxOrderList(String openCode) {
        XcxOrderExample example = new XcxOrderExample();
        example.createCriteria().andOpenCodeEqualTo(openCode);
        return xcxOrderMapper.selectByExample(example);
    }

    public int upPtOrderStatus(Integer id, Integer status) {
        XcxOrderExample example = new XcxOrderExample();
        example.createCriteria().andOrderIdEqualTo(id);
        XcxOrder xcxOrder = new XcxOrder();
        xcxOrder.setOrderId(id);
        xcxOrder.setOrderStatus(status);
        xcxOrder.setUpdateTime(new Date());
        return xcxOrderMapper.updateByExampleSelective(xcxOrder,example);
    }

    public Integer upPtOrderTransaction(Integer id, String transaction) {
        XcxOrderExample example = new XcxOrderExample();
        example.createCriteria().andOrderIdEqualTo(id);
        XcxOrder xcxOrder = new XcxOrder();
        xcxOrder.setOrderId(id);
        xcxOrder.setTransactionNo(transaction);
        xcxOrder.setUpdateTime(new Date());
        return xcxOrderMapper.updateByExampleSelective(xcxOrder,example);
    }

    public XcxOrder getOrderById(Integer orderId) {
        return xcxOrderMapper.selectByPrimaryKey(orderId);
    }

    public Long getOrderCountByStatus(Integer customerId, Integer status) {
        XcxOrderExample example = new XcxOrderExample();
        XcxOrderExample.Criteria criteria = example.createCriteria();
        switch (status) {
            case 0:
                criteria.andOrderStatusEqualTo(XcxOrderStatusConst.NO);
                break;
            case 1:
                criteria.andOrderStatusEqualTo(XcxOrderStatusConst.PT_ING);
                break;
            case 2:
                criteria.andOrderStatusEqualTo(XcxOrderStatusConst.PAID);
                break;
            case 3:
                criteria.andOrderStatusEqualTo(XcxOrderStatusConst.DELIVERED);
                break;
            case 4:
                criteria.andOrderStatusEqualTo(XcxOrderStatusConst.RECEIVED);
                break;
            case 5:
                criteria.andOrderStatusEqualTo(XcxOrderStatusConst.CANCELED);
                break;
            default:
                AssertUtils.requireEquals(XcxOrderStatusConst.NO <= status, XcxOrderStatusConst.CANCELED >= status, "状态量异常");
                break;
        }
        criteria.andUserIdEqualTo(customerId);
        return countByExample(example);
    }

    public void updateOrderShop(Integer OrderId, Integer shopId) {
        XcxOrderExample example = new XcxOrderExample();
        example.createCriteria().andOrderIdEqualTo(OrderId);
        XcxOrder xcxOrder = new XcxOrder();
        xcxOrder.setOrderId(OrderId);
        xcxOrder.setOrderId(OrderId);
        xcxOrder.setShopId(shopId);
        xcxOrderMapper.updateByExampleSelective(xcxOrder,example);
    }

    public void upPtOrderStatusAndUpdateTime(Integer orderId, Integer status) {
        XcxOrder xcxOrder  = selectByPrimaryKey(orderId);
        xcxOrder.setOrderId(orderId);
        xcxOrder.setOrderStatus(status);
        xcxOrder.setUpdateTime(new Date());
        updateByPrimaryKey(xcxOrder);
    }

    public void updateWaitPayXcxOrder() {
        //查询状态为0（待付款）的订单
        List<XcxOrder> xcxOrderList = getXcxOrderByStatus(0);
        //订单创建1小时之后，更改状态为5（已取消）
        for (XcxOrder xcxOrder : xcxOrderList) {
            if (System.currentTimeMillis() - xcxOrder.getCreateTime().getTime() > 60 * 60 * 1000) {
                upPtOrderStatus(xcxOrder.getOrderId(), 5);
            }
        }
    }

    public List<XcxOrder> getXcxOrderByStatus(Integer status) {
        XcxOrderExample example = new XcxOrderExample();
        example.createCriteria().andOrderStatusEqualTo(status);
        return xcxOrderMapper.selectByExample(example);
    }

    public void xcxConfirmOrder(List<XcxOrder> xcxOrderList) {
        for (XcxOrder xcxOrder : xcxOrderList) {
            Date nowDate = new Date();
            Date createDate = xcxOrder.getCreateTime();
            Long time = nowDate.getTime() - createDate.getTime();
            Long time2 = new Long((long) 72 * 60 * 60 * 1000);
            if (time > time2) {
                //upPtOrderStatus(xcxOrder.getOrderId(), 4);
                updateOrderRecode(xcxOrder);
            }
        }
    }

    public void updateOrderRecode(XcxOrder xcxOrder) {
        xcxOrder.setReceivedRecord(new Byte("0"));
        xcxOrder.setUpdateTime(new Date());
        xcxOrder.setOrderStatus(4);
        updateByPrimaryKey(xcxOrder);
    }

    public void updateOverTimeXcxPtOrder(List<XcxOrder> xcxOrderList) {
        Long time2 = new Long((long) 24 * 60 * 60 * 1000);
        for (XcxOrder xcxOrder : xcxOrderList) {
            Long time1 = System.currentTimeMillis() - xcxOrder.getCreateTime().getTime();
            if (time1 > time2) {
                if (xcxOrder.getPayTime() != null) {
                    upPtOrderStatus(xcxOrder.getOrderId(), 6);
                }
            }
        }
    }

    public XcxOrder getOrderByOrderCode(String xcxOrderCode) {
        XcxOrderExample example = new XcxOrderExample();
        example.createCriteria().andOrderCodeEqualTo(xcxOrderCode);
        return getByExample(example);
    }

    public XcxOrder getOrderByGroupId(Integer groupId) {
        XcxOrderExample example = new XcxOrderExample();
        example.createCriteria().andGroupIdEqualTo(groupId);
        return getByExample(example);
    }

    public Integer cancelOrder(Integer userId,Integer orderId){
        XcxOrderExample example = new XcxOrderExample();
        example.createCriteria().andUserIdEqualTo(userId).andOrderIdEqualTo(orderId);
        XcxOrder xcxOrder = new XcxOrder();
        xcxOrder.setOrderStatus(5);
        return xcxOrderMapper.updateByExampleSelective(xcxOrder, example);
    }

    public List<XcxOrder> aggregate(Integer serviceNum, Date startTime, Date endTime) {
        XcxOrderExample example = new XcxOrderExample();
        example.createCriteria().andServicenumEqualTo(serviceNum).andCreateTimeGreaterThanOrEqualTo(startTime).andCreateTimeLessThan(endTime);
        return xcxOrderMapper.selectByExample(example);
    }

    public List<XcxOrder> getOrderByOrderCodeList(List<String> xcxOrderCodes) {
        XcxOrderExample example = new XcxOrderExample();
        example.createCriteria().andOrderCodeIn(xcxOrderCodes);
        return xcxOrderMapper.selectByExample(example);
    }
}
